package zuoye;

import util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Loginjpanel extends JPanel implements ActionListener {
    private Icon loginimage = Util.getIcon("/images/buttonLand.jpg");
    private Icon offimage = Util.getIcon("/images/buttonOff.jpg");
    private JButton jButton2;
    private Icon loginimage1 = Util.getIcon("/images/buttonLogin.jpg");
    private JButton jButton;
    private JButton jButton1;
    private JLabel namejLabel;
    private JLabel pswjLabel;
    private JTextField namejTextField;
    private JPasswordField jPasswordField;
    private Mainjframe frame;
    private int conut = 3;

    public Loginjpanel(Mainjframe frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);

        namejLabel = new JLabel("账户");
        namejLabel.setForeground(Color.BLACK);
        namejLabel.setFont(new Font("宋体", Font.BOLD, 22));
        namejLabel.setBounds(380, 430, 50, 40);
        this.add(namejLabel);

        namejTextField = new JTextField();
        namejTextField.setBounds(440, 430, 350, 40);
        this.add(namejTextField);

        pswjLabel = new JLabel("密码");
        pswjLabel.setForeground(Color.BLACK);
        pswjLabel.setFont(new Font("宋体", Font.BOLD, 22));
        pswjLabel.setBounds(380, 480, 50, 40);
        this.add(pswjLabel);

        jPasswordField = new JPasswordField();
        jPasswordField.setBounds(440, 480, 350, 40);
        this.add(jPasswordField);

        jButton = new JButton(loginimage);
        jButton.setBounds(380, 540, 140, 50);
        this.add(jButton);

        jButton.addActionListener(this);
        jButton1 = new JButton(offimage);
        jButton1.setBounds(720, 540, 140, 50);
        this.add(jButton1);
        jButton1.addActionListener(this);

        jButton2 = new JButton(loginimage1);
        jButton2.setBounds(550, 540, 140, 50);
        this.add(jButton2);
        jButton2.addActionListener(this);
    }

    public int getConut() {
        return conut;
    }

    public void setConut(int conut) {
        this.conut = conut;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == jButton) {
            ArrayList<User> user = (ArrayList<User>) FileUtil.readObject("User.dat");
            for (int i = 0; i < user.size(); i++) {
                char[] account = jPasswordField.getPassword();
                String s = String.valueOf(account);
                if (user.get(i).getUsername().equals(namejTextField.getText()) && user.get(i).getUserpsw().equals(s)) {
                    frame.setIndex(i);
                    JOptionPane.showMessageDialog(this, "登陆成功");
                    namejTextField.setText("");
                    jPasswordField.setText("");
                    frame.switchPanel("menu");
                    return;
                }
            }
            conut--;
            if (conut == 0) {
                JOptionPane.showMessageDialog(this, "错误次数过多，账号已冻结");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "登录错误，账号或密码不正确，还剩" + conut + "次机会");
            namejTextField.setText("");
            jPasswordField.setText("");

        } else if (e.getSource() == jButton1) {
            System.exit(0);
        } else if (e.getSource() == jButton2) {
            frame.switchPanel("Zhuce");
        }
    }
}